package me.zhengjie.modules.management.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.ConferenceRoom;
import me.zhengjie.modules.management.domain.ConferenceType;
import me.zhengjie.modules.management.domain.Pageable;
import me.zhengjie.modules.management.mapper.ConferenceRoomMapper;
import me.zhengjie.modules.management.mapper.ConferenceTypeMapper;
import me.zhengjie.modules.management.service.ConferenceTypeService;
import me.zhengjie.modules.management.service.dto.ConferenceRoomQueryCriteria;
import me.zhengjie.modules.management.service.dto.ConferenceTypeQueryCriteria;
import me.zhengjie.utils.PageHelpResultUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: yuchao.wang
 * @Date: 2020/12/7
 * @Description :
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ConferenceTypeServiceImpl implements ConferenceTypeService {

    private final ConferenceTypeMapper conferenceTypeMapper;

    @Override
    public Map<String, Object> findByCriteria(ConferenceTypeQueryCriteria conferenceTypeQueryCriteria, Pageable pageable) {
        PageHelper.startPage(pageable.getPage(), pageable.getSize());
        List<ConferenceType> conferenceTypeList = conferenceTypeMapper.queryByCriteria(conferenceTypeQueryCriteria);
        PageInfo<ConferenceType> pageInfo = new PageInfo(conferenceTypeList);
        return PageHelpResultUtil.toPage(pageInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConferenceType resources) {

        // 字段非空校验
        nullTypeTest(resources);
        try {
            conferenceTypeMapper.insertConferenceType(resources);
        } catch (Exception e) {
            throw new BadRequestException("新增操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConferenceType resources) {

        // 字段非空校验
        nullTypeTest(resources);
        // 编辑职位
        try {
            conferenceTypeMapper.updateConferenceType(resources);
        } catch (RuntimeException e) {
            throw new BadRequestException("更新操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        // 删除
        try {
            for (Integer id : ids) {
                conferenceTypeMapper.deleteConferenceType(id);
            }
        } catch (RuntimeException e) {
            throw new BadRequestException("删除操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ConferenceType> findById(Integer typeId){
        return conferenceTypeMapper.findById(typeId);
    }
    /**
     * 对字段进行非空校验
     *
     * @param resources
     * @return void
     */
    public void nullTypeTest(ConferenceType resources) {

        if (ObjectUtil.isNull(resources.getTypeName())) {
            throw new BadRequestException("类型名称不能为空！");
        }

        if (ObjectUtil.isNull(resources.getMaxPeoples())) {
            throw new BadRequestException("最多人数不能为空！");
        }

        if (ObjectUtil.isNull(resources.getMinPeoples())) {
            throw new BadRequestException("最少人数不能为空！");
        }
    }
}
